<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="数据源" prop="datasourceId">
        <el-select
          v-model="queryParams.datasourceId"
          placeholder="请选择数据源"
          clearable
          size="small"
          style="width: 240px"
          @change="change"
        >
          <el-option
            v-for="datasource in datasourceList"
            :key="datasource.id"
            :label="datasource.datasourceName"
            :value="datasource.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="表名" prop="tableName">
        <el-input v-model="queryParams.tableName" placeholder="请输入表名" style="width:150px">></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-download" size="small" @click="handleSync" v-if="showSync">同步</el-button>
        <el-button icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
<!--        <el-button type="primary" icon="el-icon-delete" size="small" @click="handleDel" v-if="showDel">删除</el-button>-->
        <el-button icon="el-icon-refresh" size="small" @click="handleRefresh" v-if="showSync">刷新</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableAliasList"
      row-key="id"
      border
      @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="40" align="center" />-->
      <el-table-column prop="tableName" label="表名" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="tableNameAlias" label="数据表别名" :show-overflow-tooltip="true" ></el-table-column>
      <el-table-column prop="tableDesc" label="数据表说明" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="status" label="启用" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="2"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="80">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >修改
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-form-item label="数据表别名" prop="tableNameAlias">
            <el-input v-model="form.tableNameAlias" placeholder="请输入数据表别名"></el-input>
          </el-form-item>
          <el-form-item label="数据表说明" prop="tableDesc">
            <el-input type="textarea" v-model="form.tableDesc"></el-input>
          </el-form-item>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {listDatasource} from '@/api/metadata/datasource';
  import {listTableAlias, getTableAlias, delTableAlias, updateTableAlias, syncTables } from '@/api/metadata/tablealias';

  export default {
    name: 'ColAlias',
    data(){
      return {
        // 遮罩层
        loading: false,
        total: 0,
        title: '',
        open: false,
        showSync: false,
        showDel: false,
        ids: [],
        tableAliasList: [],
        datasourceList: [],
        queryParams:{
          current: 1,
          size: 10,
          datasourceId: undefined
        },
        form: {},
        rules: {
          tableNameAlias: [
            { max: 200, message: '最大长度为200个字符', trigger: 'blur' }
          ],
          tableDesc: [
            { max: 500, message: '最大长度为500个字符', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      listDatasource({ current: 1, size: 10000}).then(response => {
        this.datasourceList = response.data.records;
      });
    },
    methods: {
      handleSelectionChange(selection){
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      handleSync(){
        this.form.datasourceId = this.queryParams.datasourceId;
        syncTables(this.form).then(response => {
          if(response.code ===0){
            this.msgSuccess('同步成功');
            this.getList();
          }else{
            this.msgError(response.msg);
          }
        })
      },
      handleDel(){},
      handleDelete(row){

      },
      handleUpdate(row){
        this.reset();
        const id = row.id;
        getTableAlias(id).then(response => {
          this.form = response.data;
          this.open = true;
        });
      },
      handleStatusChange(row){
        let text = row.status === "1" ? "启用" : "停用";
        this.reset();
        this.form = {
          id: row.id,
          status: row.status
        };
        updateTableAlias(this.form).then(response => {
          if (response.code === 0)
            this.msgSuccess( text + "成功");
        })

      },
      handleRefresh(){
        this.queryParams.current = 1;
        this.getList();
      },
      submitForm(){
        this.$refs['form'].validate(valid => {
          if (valid) {
            updateTableAlias(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          }
        });
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          datasourceId: undefined,
          databaseName: undefined,
          tableName: undefined,
          tableNameAlias: undefined,
          tableDesc: undefined,
          status: undefined
        };
        this.resetForm("form");
      },

      getList(){
        this.loading = true;
        listTableAlias(this.queryParams).then(response => {
          this.tableAliasList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        })
      },
      change(val) {
        if(this.queryParams.likes)
          this.queryParams.pop(likes)
        this.queryParams.datasourceId = val;
        this.getList();
        this.showSync = true;
      },
      handleQuery(){
        this.queryParams.likes = 'tableName'
        this.getList()
      }
    }
  }
</script>

<style scoped>

</style>
